package controllers;

import models.EditRequest;
import models.Notification;
import models.Tutorial;
import play.mvc.*;

public class EditRequests extends Controller {

	/**
	 * This method takes as an input erID, and renders the tutorial of the edit
	 * request and the edit request to the editRequestPage view
	 * 
	 * @param editRequestId
	 *            which is the id of the current edit request.
	 * 
	 * @return object tutorial and object edit request
	 * 
	 * @author Marwa
	 */
	public static void editRequestPage(long editRequestId) {
		EditRequest editRequest = EditRequest.findById(editRequestId);
		long tutorialId = editRequest.tutorialId;
		Tutorial tutorial = Tutorial.findById(tutorialId);
		render(tutorial, editRequest);
	}

	/**
	 * This method enables the contributor/creator of a tutorial to accept edit
	 * tutorial request. It takes as an input erID and render a text that the
	 * user accepted the edit request
	 * 
	 * @param editRequestId
	 *            which is the id of the current edit request.
	 * 
	 * @return rendered Text if the user has already replied to the request and
	 *         if the user accepted the edit request
	 * 
	 * @author Marwa
	 */
	public static void acceptEditRequest(long editRequestId) {
		EditRequest editRequest = EditRequest.findById(editRequestId);
		if (editRequest.isChecked == false) {
			String reqContent = editRequest.edit;
			long tutorialId = editRequest.tutorialId;
			Tutorial tutorial = Tutorial.findById(tutorialId);
			tutorial.setContent(reqContent);
			editRequest.setApproved(true);
			editRequest.isChecked = true;
			tutorial.save();
			editRequest.save();
			new Notification(editRequest.replierId, editRequest.requesterId,
					tutorialId, 6).save();
			renderText("You have accepted this Edit Request");
		} else {
			renderText("You have already replied to this Edit Request");
		}
	}

	/**
	 * This method enables the contributor/creator of a tutorial to reject edit
	 * tutorial request. It takes as an input erID and render a text that the
	 * user rejected the edit request
	 * 
	 * @param editRequestId
	 *            which is the id of the current edit request.
	 * 
	 * @return rendered Text if the user has already replied to the request and
	 *         if the user rejected the edit request
	 * 
	 * @author Marwa
	 */
	public static void rejectEditRequest(long editRequestId) {
		EditRequest editRequest = EditRequest.findById(editRequestId);
		if (editRequest.isChecked == false) {
			editRequest.setApproved(false);
			editRequest.isChecked = true;
			editRequest.save();
			renderText("You have rejected this Edit Request");
		} else {
			renderText("You have already replied to this Edit Request");
		}
	}

}
